package com.study.english.service.impl;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.lang.WeightRandom;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.databind.ser.Serializers;
import com.study.english.constant.SecurityConstants;
import com.study.english.context.SecurityContextHolder;
import com.study.english.domain.SysConfig;
import com.study.english.domain.SysUserStudyTime;
import com.study.english.exception.BusinessException;
import com.study.english.pojo.vo.SysUserVo;
import com.study.english.pojo.vo.SysVideoVo;
import com.study.english.result.Result;
import com.study.english.service.*;
import com.zaxxer.hikari.util.ConcurrentBag;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

import org.springframework.beans.BeanUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.study.english.domain.SysUser;
import com.study.english.mapper.SysUserMapper;
import org.springframework.stereotype.Service;

/**
 * @author wf
 * @description 针对表【sys_user】的数据库操作Service实现
 * @createDate 2024-03-05 10:16:46
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
        implements ISysUserService {

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysVideoTypeService sysVideoTypeService;

    @Autowired
    private ISysVideoService sysVideoService;
    @Autowired
    private ISysUserStudyTimeService sysUserStudyTimeService;

    @Autowired
    private ISysConfigService sysConfigService;

    @Override
    public void addSysUser(SysUserVo sysUserVo) {
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(sysUserVo, sysUser);
        sysUser.setIsDelete(0);
        sysUser.setCreator(SecurityContextHolder.getUserId());
        sysUser.setCreateTime(new Date());
        sysUserService.save(sysUser);
    }

    @Override
    public IPage<SysUserVo> getSysUserPageList(IPage<SysUserVo> page, SysUserVo sysUserVo) {
        return this.baseMapper.getSysUserPageList(page, sysUserVo);
    }


    @Override
    public List<SysUserVo> getSysUserList(SysUserVo sysUserVo) {
        return this.baseMapper.getSysUserList(sysUserVo);

    }

    @Override
    public SysUser getSysUserById(String id) {
        return sysUserService.getById(id);
    }


    @Override
    public void delSysUser(String id) {
        sysUserService.removeById(id);
    }

    @Override
    public SysUser login(SysUserVo sysUserVo) {
        SysUser sysUser = sysUserService.getOne(new QueryWrapper<SysUser>().eq("user_name", sysUserVo.getUserName()).eq("password", sysUserVo.getPassword()));
        if (sysUser == null) {
            throw new BusinessException("用户名或者密码错误！");
        }
        return sysUser;
    }

    @Override
    public SysUserVo info() {
        SysUser sysUser = sysUserService.getById(SecurityContextHolder.getUserId());
        SysUserVo sysUserVo = new SysUserVo();
        BeanUtils.copyProperties(sysUser, sysUserVo);
        return sysUserVo;
    }

    @Override
    public SysUserStudyTime checkParentPassword(String password) {
        Long userId = SecurityContextHolder.getUserId();
        SysUser sysUser = sysUserService.getById(userId);
        String parentPassword = sysUser.getParentPassword();
        if (parentPassword == null) {
            throw new BusinessException("请设置家长密码！");
        }

        if (!parentPassword.equals(password)) {
            throw new BusinessException("密码错误！");
        }
        Calendar calendar = Calendar.getInstance();
        Date startTime = calendar.getTime();
        SysConfig sysConfig = sysConfigService.getOne(new QueryWrapper<SysConfig>().eq("user_id", userId).eq("code", "study_time"));

        if (sysConfig == null) {
            throw new BusinessException("请设置学习时长！");
        }
        calendar.add(Calendar.MINUTE, Integer.parseInt(sysConfig.getVal()));
        Date endTime = calendar.getTime();
        SysUserStudyTime sysUserStudyTime = new SysUserStudyTime();
        sysUserStudyTime.setUserId(userId);
        sysUserStudyTime.setStartTime(startTime);
        sysUserStudyTime.setEndTime(endTime);
        sysUserStudyTime.setRealName(SecurityContextHolder.getRealName());
        sysUserStudyTimeService.save(sysUserStudyTime);
        return sysUserStudyTime;
    }

    @Override
    public void updateSysUser(SysUserVo sysUserVo) {
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(sysUserVo, sysUser);
        sysUserService.updateById(sysUser);
    }

    //是否播放广告
    public Map adCheckPlay() {
        Map map = new HashMap();
        SysVideoVo sysVideoVoPlay = null;
        Long userId = SecurityContextHolder.getUserId();
        Integer watchRate = 0;
        SysConfig sysConfig = sysConfigService.getOne(new QueryWrapper<SysConfig>().eq("user_id", userId).eq("code", "ad_play_rate"));
        if (sysConfig != null && Strings.isNotEmpty(sysConfig.getVal())) {
            watchRate = Integer.parseInt(sysConfig.getVal());
        }
        Integer notWatchRate = 100 - watchRate;
        WeightRandom.WeightObj<String> weightRandomWatch = new WeightRandom.WeightObj<String>("T", watchRate);
        WeightRandom.WeightObj<String> notWeightRandomWatch = new WeightRandom.WeightObj<String>("F", notWatchRate);
        List<WeightRandom.WeightObj<String>> list = new ArrayList<WeightRandom.WeightObj<String>>();
        list.add(weightRandomWatch);
        list.add(notWeightRandomWatch);
        WeightRandom<String> stringWeightRandom = RandomUtil.weightRandom(list);
        String str = stringWeightRandom.next().toString();
        Boolean boo = false;
        if ("T".equals(str)) {
            boo = true;
        }
        if (boo) {
            SysVideoVo sysVideoVo = new SysVideoVo();
            sysVideoVo.setUserId(userId);
            sysVideoVo.setStatus(1);
            sysVideoVo.setVideoTypeStatus(1);
            sysVideoVo.setSysVideoType(1);
            List<SysVideoVo> sysVideoVoList = sysVideoService.getSysVideoCurrStudyList(sysVideoVo);
            if (sysVideoVoList != null && sysVideoVoList.size() != 0) {
                Integer randomInt = RandomUtil.randomInt(0, sysVideoVoList.size());
                sysVideoVoPlay = sysVideoVoList.get(randomInt);
            } else {
                boo = false;
            }

        }
        map.put("playFlag", boo);
        map.put("adVideo", sysVideoVoPlay);
        return map;
    }
}




